package com.sduept.nwld.dataserver.controller.config;

import com.sduept.bigdata.fault.entity.DataFilterConfig;
import com.sduept.bigdata.fault.utils.DataFilterConfigCache;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.config.DataFilterConfigManager;
import com.sduept.utils.PingYinUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Named
@ViewScoped
public class DataFilterConfigController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5383504766732499178L;

	@Autowired
	private DataFilterConfigManager manager;
	@Autowired
	private DataFilterConfigCache cache;

	private DataFilterConfig current = new DataFilterConfig();
	private List<DataFilterConfig> dataConfigs = new ArrayList<DataFilterConfig>();
	private DataFilterConfig selected = null;
	private String searchName = null;

	@PostConstruct
	public void init() {
		reset();
		dataConfigs = manager.listAllConfigs();
		Collections.sort(dataConfigs, new Comparator<DataFilterConfig>() {
			@Override
			public int compare(DataFilterConfig o1, DataFilterConfig o2) {
				return PingYinUtil.getPingYin(o1.getName()).compareTo(PingYinUtil.getPingYin(o2.getName()));
			}
		});
	}

	private void reset(){
		searchName = "";
		selected = null;
		dataConfigs.clear();
	}
	
	public void create() {
		current = new DataFilterConfig();
	}

	public void edit() {
		current = selected;
	}

	public int getIndex(DataFilterConfig config) {
		return dataConfigs.indexOf(config) + 1;
	}

	public void save() {
		boolean isCreate = false;
		if (StringUtils.isEmpty(current.getId())) {
			isCreate = true;
		}
		if (isCreate) {
			if (current != null) {
				String creatName = current.getName();
				for (DataFilterConfig i : dataConfigs) {
					if (creatName.equals(i.getName())) {
						addErrorMessage("当前规则已存在，请修改规则名称");
						return;
					}
				}
				current = manager.save(current);
				addSuccessMessage("添加成功！");
				dataConfigs.add(current);
			}
		} else {
			current = manager.save(current);
			addSuccessMessage("编辑成功！");
			selected = null;
		}
		if (StringUtils.isNotEmpty(current.getName())) {
			cache.getMap().put(current.getId(), current);
		}

	}

	public void deleteById() {
		DataFilterConfig config = manager.getConfigById(selected.getId());
		if (config.getName() != null) {
			cache.getMap().remove(selected.getId());
		}
		manager.deleteConfigById(selected.getId());
		dataConfigs.remove(selected);
		addSuccessMessage("删除成功！");
		selected = null;
	}

	public void queryByName() {
		if (StringUtils.isNotEmpty(searchName)) {
			dataConfigs = manager.findByName(searchName);
		} else {
			dataConfigs = manager.listAllConfigs();
		}
	}

	public DataFilterConfig getCurrent() {
		return current;
	}

	public void setCurrent(DataFilterConfig current) {
		this.current = current;
	}

	public List<DataFilterConfig> getDataConfigs() {
		return dataConfigs;
	}

	public void setDataConfigs(List<DataFilterConfig> dataConfigs) {
		this.dataConfigs = dataConfigs;
	}

	public DataFilterConfig getSelected() {
		return selected;
	}

	public void setSelected(DataFilterConfig selected) {
		this.selected = selected;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

}
